<template>

  <div
    style="width:100%;height:240px;"
    id="pie"
  ></div>

</template>

<script>
import echarts from "echarts";

export default {
  name: "dataSourcePie",
  props: {
    PieDataSource: {
      type: Array,
      default: function() {
        return [
        { alarmStat: "未确认", count: 0 },
        { alarmStat: "已确认", count: 0 }
        ];
      }
    }
  },
  data() {
    return {
      dataSource: [
        { alarmStat: "未确认", count: 1 },
        { alarmStat: "已确认", count: 1 }
      ]
    };
  },
  watch: {
    PieDataSource(curVal, oldVal) {
      let vm = this;
      curVal.forEach(val => {
        let alarmStat = val.alarmStat;
        vm.dataSource[
          vm.dataSource.findIndex(dVal => dVal.alarmStat == alarmStat)
        ] = val;
      });
      this.initEchats();
    }
  },
  mounted() {},
  methods: {
    initEchats() {
      var PieDataSource = echarts.init(document.getElementById("pie"));
      let option = {
        color: ["#ffd800", "#f67504"],
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
          orient: "vertical",
          x: "left",
          data: [this.dataSource[0].alarmStat, this.dataSource[1].alarmStat]
        },
        series: [
          {
            name: "告警处理",
            type: "pie",
            radius: ["50%", "70%"],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: "center"
              },
              emphasis: {
                show: true,
                textStyle: {
                  fontSize: "30",
                  fontWeight: "bold"
                }
              }
            },
            labelLine: {
              normal: {
                show: true
              }
            },
            data: [
              {
                value: this.dataSource[0].count,
                name: this.dataSource[0].alarmStat
              },
              {
                value: this.dataSource[1].count,
                name: this.dataSource[1].alarmStat
              }
            ]
          }
        ]
      };

      PieDataSource.setOption(option);
      window.addEventListener("resize", function() {
        PieDataSource.resize();
      });
    }
  }
};
</script>
